﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data;
using System.Data.OleDb;
using iShoppingDTO;
using iShoppingBUS;

namespace iShopping.Member
{
    public partial class DangSanPham : System.Web.UI.Page
    {
        private const string MDBFILE = "FileUpload.mdb";
        protected void Page_PreInit(object sender, EventArgs e)
        {
            GiaoDienDTO giaoDienDto = GiaoDienBUS.SelectGiaoDienMa("Theme001");
            if (giaoDienDto != null)
            {
                this.Theme = giaoDienDto.TenTheme;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            List<LoaiSanPhamDTO> dsLSP = LoaiSanPhamBUS.LayDSLoaiSanPham();
            DropDownList1.DataSource = LoaiSanPhamBUS.LayDSLoaiSanPhamO();
            DropDownList1.DataTextField = "TenLoaiSanPham";
            DropDownList1.DataValueField = "TenLoaiSanPham";
            DropDownList1.DataBind();

            String maTaiKhoan = Request.QueryString["MTK"].ToString();
            Session.Add("MaTaiKhoan", maTaiKhoan);
        }

        private void WriteToFile(string strPath, ref byte[] Buffer)
        {
            FileStream newFile = new FileStream(strPath, FileMode.Create);
            newFile.Write(Buffer, 0, Buffer.Length);
            newFile.Close();
        }

        protected void btnViewVid_Click1(object sender, EventArgs e)
        {
            string mayoutube = TBLinkVideo.Text;
            mayoutube = mayoutube.Substring(mayoutube.IndexOf("watch?v=") + 8, 11);
            Session.Add("vidlink", mayoutube);
        }

        private string UpToServ(HtmlInputFile filMyFile, Image imgFile, string filename)
        {
            filename += (string)Session["MaTaiKhoan"];
            HttpPostedFile myFile = filMyFile.PostedFile;
            int nFileLen = myFile.ContentLength;
            string strFilename = "";
            if (nFileLen > 0)
            {
                byte[] myData = new byte[nFileLen];
                myFile.InputStream.Read(myData, 0, nFileLen);
                string ext = Path.GetExtension(myFile.FileName);
                filename += ext;
                strFilename = @"/data/images/" + filename;
                WriteToFile(Server.MapPath(strFilename), ref myData);

                imgFile.ImageUrl = strFilename;
                imgFile.Width = 150;
                imgFile.Height = 150;
                imgFile.Visible = true;
            }

            return strFilename;
        }

        protected void btnViewImg_Click(object sender, EventArgs e)
        {
            long filename = DateTime.Now.Ticks;
            if (filMyFile1.PostedFile.FileName != "")
            {
                filename++;
                string imagepath = UpToServ(filMyFile1, imgFile1, filename.ToString());
                Session.Add("Image1", imagepath);
            }
            if (filMyFile2.PostedFile.FileName != "")
            {
                filename++;
                string imagepath = UpToServ(filMyFile2, imgFile2, filename.ToString());
                Session.Add("Image2", imagepath);
            }
            if (filMyFile3.PostedFile.FileName != "")
            {
                filename++;
                string imagepath = UpToServ(filMyFile3, imgFile3, filename.ToString());
                Session.Add("Image3", imagepath);
            }
            if (filMyFile4.PostedFile.FileName != "")
            {
                filename++;
                string imagepath = UpToServ(filMyFile4, imgFile4, filename.ToString());
                Session.Add("Image4", imagepath);
            }
            if (filMyFile5.PostedFile.FileName != "")
            {
                filename++;
                string imagepath = UpToServ(filMyFile5, imgFile5, filename.ToString());
                Session.Add("Image5", imagepath);
            }
            
        }

        protected void cmdSend_Click1(object sender, EventArgs e)
        {
            SanPhamDTO sp = new SanPhamDTO();
            sp.GiaKhoiDiem = int.Parse(TBGia.Text);
            if (Session["Image1"] != null)
            {
                sp.HinhAnh = (string)Session["Image1"];
            }
            else if (Session["Image2"] != null)
            {
                sp.HinhAnh = (string)Session["Image2"];
            }
            else if (Session["Image3"] != null)
            {
                sp.HinhAnh = (string)Session["Image3"];
            }
            else if (Session["Image4"] != null)
            {
                sp.HinhAnh = (string)Session["Image4"];
            }
            else if (Session["Image5"] != null)
            {
                sp.HinhAnh = (string)Session["Image5"];
            }
            else
            {
                sp.HinhAnh = "";
            }

            sp.MaLoaiSanPham = LoaiSanPhamBUS.LayMaTheoTen(DropDownList1.Text);
            sp.MaTaiKhoan = (string)Session["MaTaiKhoan"];
            sp.MaTinhTrangSP = 1;
            sp.MoTaSanPham = TBMoTa.Text;
            sp.TenSanPham = TBTenSP.Text;
            sp.NgayDang = DateTime.Now;
            sp.NgayHetHan = sp.NgayDang.Add(new TimeSpan(int.Parse(TBThoiGian.Text), 0, 0));

            List<MultimediaDTO> lmt = new List<MultimediaDTO>();
            if (Session["Image1"] != null)
            {
                MultimediaDTO mt = new MultimediaDTO();
                mt.TenMT = Path.GetFileName((string)Session["Image1"]);
                mt.DungLuong = "1";
                mt.KichThuoc = "1x1";
                mt.Link = (string)Session["Image1"];
                mt.MaLoaiMT = 1;
                lmt.Add(mt);
            }

            if (Session["Image2"] != null)
            {
                MultimediaDTO mt = new MultimediaDTO();
                mt.TenMT = Path.GetFileName((string)Session["Image2"]);
                mt.DungLuong = "1";
                mt.KichThuoc = "1x1";
                mt.Link = (string)Session["Image2"];
                mt.MaLoaiMT = 1;
                lmt.Add(mt);
            }

            if (Session["Image3"] != null)
            {
                MultimediaDTO mt = new MultimediaDTO();
                mt.TenMT = Path.GetFileName((string)Session["Image3"]);
                mt.DungLuong = "1";
                mt.KichThuoc = "1x1";
                mt.Link = (string)Session["Image3"];
                mt.MaLoaiMT = 1;
                lmt.Add(mt);
            }

            if (Session["Image4"] != null)
            {
                MultimediaDTO mt = new MultimediaDTO();
                mt.TenMT = Path.GetFileName((string)Session["Image4"]);
                mt.DungLuong = "1";
                mt.KichThuoc = "1x1";
                mt.Link = (string)Session["Image4"];
                mt.MaLoaiMT = 1;
                lmt.Add(mt);
            }

            if (Session["Image5"] != null)
            {
                MultimediaDTO mt = new MultimediaDTO();
                mt.TenMT = Path.GetFileName((string)Session["Image5"]);
                mt.DungLuong = "1";
                mt.KichThuoc = "1x1";
                mt.Link = (string)Session["Image5"];
                mt.MaLoaiMT = 1;
                lmt.Add(mt);
            }

            MultimediaDTO mt2 = new MultimediaDTO();
            mt2.DungLuong = "1";
            mt2.KichThuoc = "1x1";
            mt2.Link = TBLinkVideo.Text;
            mt2.MaLoaiMT = 2;
            mt2.TenMT = "Video giới thiệu";
            lmt.Add(mt2);

            SanPhamBUS.ThemSanPham(sp, lmt);

            //Session.Clear();
        }
    }
}
